package com.mall.controller.portal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mall.common.Const;
import com.mall.common.ResponseCode;
import com.mall.common.ServerResponse;
import com.mall.pojo.MmallShipping;
import com.mall.pojo.MmallUser;
import com.mall.service.IShippingService;
import com.mall.util.CookieUtil;
import com.mall.util.JsonUtil;
import com.mall.util.RedisPoolUtil;

@Controller
@RequestMapping("/shipping")
public class ShippingController {
	
	@Autowired
	private IShippingService shippingService;
	
	@RequestMapping("/add.do")
	@ResponseBody
	public ServerResponse add(HttpServletRequest request , MmallShipping shipping ){
		
		//判断是否登录
		//MmallUser user = (MmallUser)session.getAttribute(Const.CURRENT_USER);
		
		String loginToken = CookieUtil.readLoginToken(request);

		if(StringUtils.isEmpty(loginToken)){
			return ServerResponse.createByErrorMessage("用户未登录");
		}
		
		String userStr = RedisPoolUtil.get(loginToken);
		MmallUser user = JsonUtil.string2Obj(userStr, MmallUser.class);
		
		if(user ==null){
			return ServerResponse.createByErroerCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"need_login");
		}
		
		return shippingService.insert(user.getId(),shipping);
	}
	
	@RequestMapping("/delete.do")
	@ResponseBody
	public ServerResponse delete(HttpServletRequest request,Integer shippingId){
		
		//判断是否登录
		//MmallUser user = (MmallUser)session.getAttribute(Const.CURRENT_USER);
		
		String loginToken = CookieUtil.readLoginToken(request);

		if(StringUtils.isEmpty(loginToken)){
			return ServerResponse.createByErrorMessage("用户未登录");
		}
		
		String userStr = RedisPoolUtil.get(loginToken);
		MmallUser user = JsonUtil.string2Obj(userStr, MmallUser.class);
		
		if(user ==null){
			return ServerResponse.createByErroerCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"need_login");
		}
		
		return shippingService.delete(user.getId(),shippingId);
	}
	
	@RequestMapping("/update.do")
	@ResponseBody
	public ServerResponse update(HttpServletRequest request, MmallShipping shipping ){
		
		//判断是否登录
		//MmallUser user = (MmallUser)session.getAttribute(Const.CURRENT_USER);
		
		String loginToken = CookieUtil.readLoginToken(request);

		if(StringUtils.isEmpty(loginToken)){
			return ServerResponse.createByErrorMessage("用户未登录");
		}
		
		String userStr = RedisPoolUtil.get(loginToken);
		MmallUser user = JsonUtil.string2Obj(userStr, MmallUser.class);
		
		if(user ==null){
			return ServerResponse.createByErroerCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"need_login");
		}
		
		return shippingService.update(user.getId(),shipping);
		
	}
	
	@RequestMapping("/select.do")
	@ResponseBody
	public ServerResponse select(HttpServletRequest request, Integer shippingId ){
		
		//判断是否登录
		//MmallUser user = (MmallUser)session.getAttribute(Const.CURRENT_USER);
		
		String loginToken = CookieUtil.readLoginToken(request);

		if(StringUtils.isEmpty(loginToken)){
			return ServerResponse.createByErrorMessage("用户未登录");
		}
		
		String userStr = RedisPoolUtil.get(loginToken);
		MmallUser user = JsonUtil.string2Obj(userStr, MmallUser.class);
		
		
		if(user ==null){
			return ServerResponse.createByErroerCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"need_login");
		}
		
		return shippingService.selectByUserIdShippingId(user.getId(),shippingId);
		
	}
	
	@RequestMapping("/list.do")
	@ResponseBody
	public ServerResponse list(HttpServletRequest request,@RequestParam(value="pageNum",defaultValue="1")Integer pageNum,@RequestParam(value="pageSize",defaultValue="10")Integer pageSize){
		
		//判断是否登录
		//MmallUser user = (MmallUser)session.getAttribute(Const.CURRENT_USER);
		
		String loginToken = CookieUtil.readLoginToken(request);

		if(StringUtils.isEmpty(loginToken)){
			return ServerResponse.createByErrorMessage("用户未登录");
		}
		
		String userStr = RedisPoolUtil.get(loginToken);
		MmallUser user = JsonUtil.string2Obj(userStr, MmallUser.class);
		
		if(user ==null){
			return ServerResponse.createByErroerCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"need_login");
		}
		
		return shippingService.list(user.getId(),pageNum,pageSize);
		
	}
	
	
}
